local MakePlayerCharacter = require "prefabs/player_common"

local assets = {Asset("SCRIPT", "scripts/prefabs/player_common.lua")}
local prefabs = {}

-- 初始物品 2时间碎片，2花瓣，2噩梦燃料，石榴
local start_inv = {"pocketwatch_parts", "pocketwatch_parts", "petals", "petals", "nightmarefuel", "nightmarefuel",
                   "pomegranate"}
TUNING.GAMEMODE_STARTING_ITEMS.DEFAULT.TWILIGHT_ARCHANGEL = {"pocketwatch_parts", "pocketwatch_parts", "petals",
                                                             "petals", "nightmarefuel", "nightmarefuel", "pomegranate"}
-- 当人物复活的时候
local function onbecamehuman(inst)
    -- 设置人物的移速（1表示1倍于wilson）
    -- inst.components.locomotor:SetExternalSpeedMultiplier(inst, "twilight_archangel_speed_mod", 1.25)
    if inst.components.ta_fragmentate.death then
        inst.components.ta_fragmentate.death = false
        inst.components.ta_fragmentate:DoDelta(25)

        -- if inst.components.ta_fragmentate.current > 10 then
        --     inst.components.ta_fragmentate:DoDelta(-10)
        -- else
        --     inst.components.ta_fragmentate:DoDelta(1 - inst.components.ta_fragmentate.current)
        -- end
    end
    inst.components.sanity:DoDelta(1000)

    -- inst:AddComponent("groundpounder")
    -- inst.components.groundpounder.workefficiency = 1000
end
-- 当人物死亡的时候
local function onbecameghost(inst)
    -- 变成鬼魂的时候移除速度修正
    inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "twilight_archangel_speed_mod")
end

-- 重载游戏或者生成一个玩家的时候
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- 这个函数将在服务器和客户端都会执行
-- 一般用于添加小地图标签等动画文件或者需要主客机都执行的组件（少数）
local common_postinit = function(inst)
    -- Minimap icon
    inst.MiniMapEntity:SetIcon("twilight_archangel.tex")
end

-- 这里的的函数只在主机执行  一般组件之类的都写在这里
local master_postinit = function(inst)
    -- 人物音效
    inst.soundsname = "willow"

    -- 三维	
    inst.components.health:SetMaxHealth(TUNING.TWILIGHT_ARCHANGEL_HEALTH)
    inst.components.hunger:SetMax(TUNING.TWILIGHT_ARCHANGEL_HUNGER)
    inst.components.sanity:SetMax(TUNING.TWILIGHT_ARCHANGEL_SANITY)

    -- 伤害系数
    inst.components.combat.damagemultiplier = 1.25
    -- 伤害减免。
    inst.components.health.absorb = -0.25
    -- 饥饿速度
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE

    -- 喜欢吃石榴，原版只支持料理。
    -- inst.components.foodaffinity:AddPrefabAffinity("pomegranate", TUNING.AFFINITY_15_CALORIES_SMALL)

    inst:AddTag("susceptible") -- 易伤

    inst:AddComponent("ta_fragmentate")

    local oldEaterEat = inst.components.eater.Eat
    function inst.components.eater:Eat(food, feeder)
        if food.prefab == "pomegranate" then
            -- 不能直接改食物的数值，这样会触发五倍回san
            -- print("前",food.components.edible.sanityvalue)
            -- food.components.edible.healthvalue = food.components.edible.healthvalue +
            --                                          TUNING.TWILIGHT_ARCHANGEL_POMEGRANATE_HEALTH
            -- food.components.edible.sanityvalue = food.components.edible.sanityvalue +
            --                                          TUNING.TWILIGHT_ARCHANGEL_POMEGRANATE_SAN
            -- print("后", food.components.edible.sanityvalue)
            feeder.isNotNeedSAN = true
            feeder.components.sanity:DoDelta(TUNING.TWILIGHT_ARCHANGEL_POMEGRANATE_SAN)
            feeder.isNotNeedSAN = false
            feeder.components.health:DoDelta(TUNING.TWILIGHT_ARCHANGEL_POMEGRANATE_HEALTH)

            -- 每吃十个石榴恢复3点破碎值
            if feeder.pomegranateEatCount == nil then
                feeder.pomegranateEatCount = 1
            else
                feeder.pomegranateEatCount = feeder.pomegranateEatCount + 1
            end
            if feeder.pomegranateEatCount >= 10 then
                feeder.components.ta_fragmentate:DoDelta(3)
                feeder.pomegranateEatCount = 0
            end
        end

        return oldEaterEat(self, food, feeder)
    end

    -- 免疫环境扣san
    inst.components.sanity:SetNegativeAuraImmunity(true) -- 疯狂光环
    inst.components.sanity:SetPlayerGhostImmunity(true) -- 玩家死亡
    inst.components.sanity:SetLightDrainImmune(true) -- 傍晚夜晚洞穴掉san

    -- 理智掉落与恢复效率x5倍
    local oldSanDoDelta = inst.components.sanity.DoDelta
    function inst.components.sanity:DoDelta(delta, ...)
        if not self.inst.isNotNeedSAN then
            delta = delta * 5
        end
        oldSanDoDelta(self, delta, ...)
    end

    -- 自身不会因死亡与其他原因正常降低体力上限
    local oldHealthDeltaPenalty = inst.components.health.DeltaPenalty

    function inst.components.health:DeltaPenalty(delta)
        -- if delta < 0 then
        --     oldHealthDeltaPenalty(self, delta)
        -- end
    end

    -- 累计受到超过100点伤害减少一点破碎值（计算护甲前的伤害） 
    -- 每损失一点破碎值体力上限减少1% 破碎值和体力值调整 
    local oldGetAttacked = inst.components.combat.GetAttacked
    function inst.components.combat:GetAttacked(attacker, damage, weapon, stimuli, spdamage, ...)
        if inst.cumulativeAttack == nil then
            inst.cumulativeAttack = 0
        end
        if damage ~= nil then
            inst.cumulativeAttack = inst.cumulativeAttack + damage
        end
        if spdamage ~= nil then
            for sptype, dmg in pairs(spdamage) do
                inst.cumulativeAttack = inst.cumulativeAttack + dmg
            end
        end
        local jie = 50
        if inst.components.ta_fragmentate.current < 50 then
            jie = 25
        end
        if inst.cumulativeAttack >= jie then
            inst.cumulativeAttack = inst.cumulativeAttack - jie
            inst.components.ta_fragmentate:DoDelta(-1)
            -- oldHealthDeltaPenalty(inst.components.health, 0.01)
        end
        oldGetAttacked(self, attacker, damage, weapon, stimuli, spdamage, ...)
    end

    -- 破碎值低于25时攻击与开采矿物伐木的行为会扣除1点破碎值
    inst:ListenForEvent("onhitother", function(inst, data)
        if inst.components.ta_fragmentate.current < 25 then
            inst.components.ta_fragmentate:DoDelta(-1)
        end
    end)

 

    inst.OnLoad = onload
    inst.OnNewSpawn = onload

end

return MakePlayerCharacter("twilight_archangel", prefabs, assets, common_postinit, master_postinit, start_inv)
